package org.gas.barapraja.proc.buffer;

import java.util.LinkedList;
import java.util.ListIterator;

public abstract class AbstractGasQueue<E> implements GasQueue<E> {

	protected LinkedList<E> objetcs=new LinkedList<E>();

	public synchronized void queue(E object){
		if(!objetcs.contains(object))
			objetcs.add(object);
	}

	public synchronized E dequeue() {
		E queued=null;
		if(size()>0){
			queued=objetcs.removeFirst();
		}
		return queued;
	}

	public synchronized E dequeue(E object) {
		E current = null;
		ListIterator<E> iterator=objetcs.listIterator();
		while(iterator.hasNext()){
			current=find(object);
			if(current!=null && objetcs.contains(object))
				objetcs.remove(current);
		}
		return current;
		
	}

	public synchronized int size() {
		return objetcs.size();
	}

	public synchronized boolean isEmpty() {
		return objetcs.isEmpty();
	}

	public synchronized E find(int index) {
		return objetcs.get(index);
	}

	public synchronized E find(E object) {
		E current;
		ListIterator<E> iterator=objetcs.listIterator();
		while(iterator.hasNext()){
			current=iterator.next();
			if(current.equals(object)){
				return current;
			}
		}
		return null;
	}

	

}
